<?php

namespace app\store\controller\market\jointly;

use app\store\controller\Controller;
use app\store\model\OrderWxpage as OrderModel;

/**
 * 优惠券管理
 * Class Coupon
 * @package app\store\controller\market
 */
class Order extends Controller
{
    /* @var CouponModel $model */
    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new OrderModel;
    }

    /**
     * 优惠券列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $query = [];
        $r_payType = input('r_payType');
        $r_state = input('r_state');
        $start_time = input('start_time');
        $end_time = input('end_time');
        $search = input('search');


        if(!empty($r_payType)){ $query['r_payType'] = $r_payType; }
        if(!empty($r_state)){ $query['r_state'] = $r_state; }
        if(!empty($start_time)){ $query['r_createtime'] = ['>',$start_time]; }
        if(!empty($end_time)){ $query['r_createtime'] = ['<',$end_time]; }
        if(!empty($search)){ $query['u_trueName|c_nickName|u_mobile'] = ['like',"%{$search}%"]; }


        //order_wxpage
        $list = $this->model->getList($query);
        //var_dump($list);
        return $this->fetch('index', compact('list'));
    }

    public function writeoff(){
        $r_id = input('r_id');
        $r = $this->model->where('r_id',$r_id)->update([
            'r_state'=>80
        ]);
        if($r!==false){
            $data = ['code'=>1,'msg'=>'核销成功！'];
        }else{
            $data = ['code'=>1,'msg'=>'核销失败，请刷新页面重试！'];
        }

        return json($data);
    }
}